/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */

// reference local blank image
Ext.BLANK_IMAGE_URL = CONTEXT_PATH + '/ext/resources/images/default/s.gif';
Ext.SSL_SECURE_URL = CONTEXT_PATH + '/blank.html';

// Encoding for ajax form POST
/** @namespace Ext.lib */
Ext.lib.Ajax.defaultPostHeader += '; charset=UTF-8';


Ext.namespace("Login");


/**
 * Login entry point.
 */
Login.app = function() {

	return {

		formPanel: null,

		window: null,

		init: function() {
			Ext.QuickTips.init();

			this.formPanel = new Ext.form.FormPanel({
				id: 'login-form',
				height: 150,
				labelWidth: 150,
				region: 'south',
				baseCls: 'x-plain',
				defaultType: 'textfield',
				frame: false,
				waitMsgTarget: true,
				standardSubmit: true,
				url: CONTEXT_PATH + '/j_spring_security_check',
				defaults: {
					width: 200
				},
				items: [{
					fieldLabel: MSG['user.login'],
					name: 'j_username',
					value: '',
					allowBlank: false,
					maxLength: 20
				},{
					fieldLabel: MSG['user.password'],
					inputType: 'password',
					name: 'j_password',
					value: '',
					maxLength: 12
				},{
					xtype: 'panel',
					id: 'logos',
					border: false,
					width: '100%',
					style:'padding: 40px 5px 0px 0px;'
				}]
			});

			this.window = new Ext.Window({
				id: 'login-win',
				title: MSG['login.title'],
				iconCls: 'icon-key',
				layout: 'border',
				width: 530,
				height: 310,
				minHeight: 250,
				minWidth: 530,
				buttonAlign: 'right',
				plain: false,
				resizable: false,
				closable: false,
				draggable: false,
				keys: {
					key: [13],
					fn: this.doLogin,
					scope: Login.app
				},
				items: [{
					id: 'login-logo',
					baseCls: 'x-plain',
					region: 'center',
					xtype: 'panel'
				},
					this.formPanel
				],
				buttons: [{
					text: MSG['button.ok'],
					iconCls: 'icon-ok',
					handler: this.doLogin,
					scope: Login.app
				}, {
					text: MSG['button.cancel'],
					iconCls: 'icon-cancel',
					handler: this.doCancel,
					scope: Login.app
				}, {
					text: MSG['button.remember'],
					iconCls: 'icon-remember',
					handler: this.doRemember,
					scope: Login.app
				}]
			});


			this.formPanel.on('render', function() {
				var f = this.formPanel.getForm().findField('j_username');
				if (f) {
					f.focus();
				}
			}, this, {delay: 200});

			this.window.show();
		},

		doLogin: function () {
			var theForm = this.formPanel.getForm();
			if(theForm.isValid()) {
				theForm.getEl().dom.action = CONTEXT_PATH + '/j_spring_security_check';
				theForm.submit();
			}
		},

		onLoginSuccess: function(form, action) {
			window.location = action.result.url;
		},

		onLoginError: function(/*form, action*/) {
			// Do nothing ?
		},

		doCancel: function() {
			var theForm = this.formPanel.getForm();
			theForm.reset();
		},

		doRemember: function() {
			var theForm = this.formPanel.getForm();

			if(theForm.isValid()) {
				Ext.Ajax.request({
					waitTitle: MSG['wait.title'],
					waitMsg: MSG['wait.message'],
					url: CONTEXT_PATH + '/remember.do',
					method: 'POST',
					success: this.onRememberSuccess,
					failure: this.onRememberError,
					params: theForm.getRawValues()
				});
			}


		},

		onRememberSuccess: function(form, action) {
			notifySuccess(MSG['user.update'], action.result.message);
		},

		onRememberError: function(form, action) {
			notifyError(MSG['user.update'], action.result.error);
		}

	};
}();
